<?php

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2018/10/13
 * Time: 17:53
 */

namespace app\index\model;


use think\Model;

class Article extends Model
{
    // 图片json解析
    public function getImagesAttr($value)
    {
        return json_decode($value);
    }

    // 图片json解析
    public function getContentAttr($value)
    {
        return json_decode($value);
    }

    // 分类名称对象
    public function getCategoriesAttr($value)
    {
        $db = db('article_categories');
        $data = $db->field('id,name')->find($value);
        return $data;
    }

    public function getContentSizeAttr($value)
    {
        $contents = json_decode($value);
        if (is_array($contents)) {
            $size = count($contents);
        } else {
            $size = 0;
        }
        return $size;
    }

    // 获取评论数
    public function getCommentCountAttr($value)
    {
        return db('comment')->where('pid', $value)->count();
    }

    // 获取点赞数
    public function getLikesCountAttr($value)
    {
        return db('collection')->where('pid', $value)->where('type', 'like')->count();
    }

    // 获取分享数
    public function getShareCountAttr($value)
    {
        return db('collection')->where('pid', $value)->where('type', 'share')->count();
    }

}